<template>
  <el-container>
    <el-header>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" @submit.prevent>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="状态">
            <el-option
              v-for="item in licenseStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button type="default" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-divider direction="horizontal"></el-divider>
    <el-main class="cus-main">
      <el-table :data="licenseList" style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          v-if="false"
        ></el-table-column>
        <el-table-column
          prop="appCode"
          label="应用编码"
          v-if="false"
        ></el-table-column>
        <el-table-column
          prop="appName"
          label="应用名"
        ></el-table-column>
        <el-table-column
          prop="appDesc"
          label="应用描述"
        ></el-table-column>
        <el-table-column
          prop="userLimit"
          label="用户数"
        ></el-table-column>
        <el-table-column
          prop="expiredAt"
          label="过期日期"
        >
        <template #default="scope">
              {{
                scope.row.expiredAt.replaceAll(" 00:00:00", "")
              }}
          </template>
      </el-table-column>
        <el-table-column prop="status" label="状态" >
          <template #default="scope">
            <el-tag :type="scope.row.status === 2 ? 'success' : 'danger'">
              {{
                licenseStatus.find((l) => l.value === scope.row.status).label
              }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { get, post, put, del } from "@/utils/request";

const licenseStatus = ref([]);
const searchForm = reactive({
  status: null,
});
const licenseList = ref([]);

const onReset = () => {
  Object.keys(searchForm).forEach((key) => {
    searchForm[key] = null;
  });
};
const onSearch = () => {
  let params = {
    status: searchForm.status,
  };
  get("/tmo/licenses/my", params)
    .then((res) => {
      // console.log("搜索结果:", res);
      if (res.data.code === 0) {
        licenseList.value = res.data.data.data;
      } else if (res.data.code === 401) {
        sessionStorage.clear();
        window.location.assign("/login");
      } else {
        ElMessage.error(res.data.msg ? res.data.msg : res.data.message);
      }
    })
    .catch((e) => {
      ElMessage.error(e.message);
    });
};


onMounted(async() => {
  // 初始化字典
  await get("/tmo/licenses/status")
    .then((res) => {
      // console.log("搜索结果:", res);
      if (res.data.code === 0) {
        licenseStatus.value = res.data.data.data;
      } else if (res.data.code === 401) {
        sessionStorage.clear();
        window.location.assign("/login");
      } else {
        ElMessage.error(res.data.msg ? res.data.msg : res.data.message);
      }
    })
    .catch((e) => {
      ElMessage.error(e.message);
    });
  onSearch();
});
</script>


